package com.kgc.server.controller;


import com.kgc.server.service.AlipayService;
import com.kgc.server.util.ResponseMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;

@Controller
public class AlipayController {

    @Autowired
    private AlipayService alipayService;

    private Logger logger = LoggerFactory.getLogger(getClass());

    @RequestMapping("toPay")
    public void toPay(HttpServletResponse resp,HttpServletRequest req){
        logger.info("AlipayController toPay start...");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html");
        String id = req.getParameter("id");
        logger.info("AlipayController toPay id:"+id);
        PrintWriter pw = null;
        ResponseMessage msg = alipayService.alipayService(req);
        logger.info("AlipayController toPay id:"+id+",msg:"+msg);
        try {
            pw = resp.getWriter();
            pw.print(msg.getData());
            logger.info("AlipayController toPay id:"+id+",msg:"+msg+",pw:"+pw);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if(pw!=null){
                pw.close();
            }
        }
    }

    @RequestMapping("returnPayMsg")
    public void returnPayMsg(HttpServletResponse resp, HttpServletRequest req){
        logger.info("AlipayController returnPayMsg start...");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html");
        ResponseMessage responseMessage = alipayService.returnPayMsg(req);
        logger.info("AlipayController returnPayMsg responseMessage:"+responseMessage);
        PrintWriter writer = null;
        try {
             writer=resp.getWriter();
             writer.print(responseMessage.getData());
            logger.info("AlipayController returnPayMsg responseMessage:"+responseMessage+",writer:"+writer);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if(writer!=null){
                writer.close();
            }
        }

    }

}
